package com.itany.book.controller;

import ch.qos.logback.core.pattern.util.RegularEscapeUtil;
import com.github.pagehelper.PageInfo;
import com.itany.book.pojo.Permission;
import com.itany.book.pojo.VOPermission;
import com.itany.book.service.PermissionService;
import com.itany.book.vo.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.IntUnaryOperator;

/**
 * Author:yaozhiyuan0117@163.com
 * Time:2019/12/26 15:57
 * Describe:
 * version:1.0
 */

@Controller
@RequestMapping("/permission")
public class PermissionController {

    @Autowired
    private PermissionService permissionService;

    @ResponseBody
    @RequestMapping("/findMenuById")
    public  List<Permission> findMenuById(Integer id){
        return permissionService.findMenuById(id);
    }

    @RequestMapping("/findTree")
    @ResponseBody
    public List<VOPermission> findTree(){
        List<VOPermission> permissions =  permissionService.findTree();
        System.out.println("-----------");
        for (VOPermission p : permissions){
            System.out.println(p);
        }
        return permissions;
    }
    @RequestMapping("/findAll")
    @ResponseBody
    public Map findAll(Integer page, Integer rows, String permissionName, String parentName){
        System.out.println("----------------");
        System.out.println(parentName);
        System.out.println(permissionName);
        Map map = new HashMap();
        PageInfo<Permission> info = permissionService.findAll(page,rows, permissionName, parentName);
        map.put("total", info.getTotal());
        map.put("rows", info.getList());
        return map;
    }

    @RequestMapping("/findById")
    @ResponseBody
    public ResponseResult findById(Integer id){
        Permission permission = permissionService.findById(id);
        return ResponseResult.success(permission);
    }

    @RequestMapping("/findAllParents")
    @ResponseBody
    public List<Permission> findAllParents(){
        List<Permission> permissions = permissionService.findAllParents();
        return permissions;
    }

    @RequestMapping("/modifyPermission")
    @ResponseBody
    public ResponseResult modifyPermission(Permission p ){
        System.out.println(p);
        permissionService.modifyPermission(p);

        return ResponseResult.success();
    }

}